package com.zx.disneyholidays.page.coupon;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.tencent.mmkv.MMKV;
import com.zx.disneyholidays.MyApplication;
import com.zx.disneyholidays.R;
import com.zx.disneyholidays.bean.CouponDetailParentBean;
import com.zx.disneyholidays.bean.TicketCardBean;
import com.zx.disneyholidays.net.BaseResponse;
import com.zx.disneyholidays.net.HttpUtils;
import com.zx.disneyholidays.net.ResponseCallBack;
import com.zx.disneyholidays.utils.StatusBarUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CouponDetailActivity extends AppCompatActivity {
    private String couponId;
    private String ticketId;
    TextView tvCurrentTime;
    TextView tvCurrentTime2;
    TextView tvcCardOwner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Object();
        setContentView(R.layout.activity_coupon_detail);

        StatusBarUtils.setColor(this, Color.WHITE);
        StatusBarUtils.setTextDark(this, true);

        Intent intent = getIntent();
        couponId = intent.getStringExtra("couponId");
        ticketId = intent.getStringExtra("ticketId");

        tvCurrentTime = findViewById(R.id.tv_current_date);
        tvCurrentTime2 = findViewById(R.id.tv_current_date_second);
        tvcCardOwner = findViewById(R.id.tv_card_owner);


        requestCouponDetail();

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CouponDetailActivity.this.finish();
            }
        });
        updateTime();
    }

    // 创建 SimpleDateFormat 对象，设置输出格式
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

    private void updateTime() {
        try {
            String time = sdf.format(new Date());
            String start = time.substring(0, time.length() - 2);
            String end = time.substring(time.length() - 2, time.length());
            tvCurrentTime.setText(start);
            tvCurrentTime2.setText(end);
        } catch (Exception e) {
            e.printStackTrace();
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    String time = sdf.format(new Date());
                    String start = time.substring(0, time.length() - 2);
                    String end = time.substring(time.length() - 2, time.length());
                    tvCurrentTime.setText(start);
                    tvCurrentTime2.setText(end);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                updateTime();
            }
        }, 1000);
    }

    private void requestCouponDetail() {
        Map<String, String> params = new HashMap<>();
        params.put("id", couponId);
        params.put("ticket_id", ticketId);
        HttpUtils.instance.httpGet("api/index/coupon_detail", params, new ResponseCallBack() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (data.getCode() != 1) {
                    Toast.makeText(MyApplication.applicationContext, data.getMsg(), Toast.LENGTH_SHORT).show();
                    return;
                }

                try {
                    Log.d("xxx", data.getData());
                    CouponDetailParentBean couponDetailBean = JSONObject.parseObject(data.getData(), CouponDetailParentBean.class);
                    bindView(couponDetailBean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(int error, String msg) {
                Toast.makeText(MyApplication.applicationContext, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void bindView(CouponDetailParentBean couponDetailBean) {
        if (couponDetailBean == null
                || couponDetailBean.getCoupon_detail() == null
                || couponDetailBean.getTicket() == null) {
            return;
        }
        ((TextView) findViewById(R.id.tv_coupon_name)).setText(couponDetailBean.getCoupon_detail().getName());
        ((TextView) findViewById(R.id.tv_coupon_date)).setText("有效期："
                + couponDetailBean.getCoupon_detail().getStart_date()
                + "-"
                + couponDetailBean.getCoupon_detail().getEnd_date());

        if (!TextUtils.isEmpty(couponDetailBean.getTicket().getQr_image())) {
            Glide.with(this)
                    .load(couponDetailBean.getTicket().getQr_image())
                    .into((ImageView) findViewById(R.id.iv_coupon));
        }

        ((TextView) findViewById(R.id.tv_card_name)).setText(couponDetailBean.getTicket().getName());
        //((TextView) findViewById(R.id.tv_card_owner)).setText(couponDetailBean.getCoupon_detail().get);
        ((TextView) findViewById(R.id.tv_card_date)).setText(couponDetailBean.getTicket().getStart_date() + "-" + couponDetailBean.getTicket().getEnd_date());
        ((TextView) findViewById(R.id.tv_card_number)).setText(couponDetailBean.getTicket().getCard_no());
        ((TextView) findViewById(R.id.tv_coupon_detail_info)).setText(Html.fromHtml(couponDetailBean.getCoupon_detail().getDesc()));
        ((TextView) findViewById(R.id.tv_coupon_instructions_info)).setText(Html.fromHtml(couponDetailBean.getCoupon_detail().getContent()));
        String name = MMKV.defaultMMKV().decodeString("userName");
        if (!TextUtils.isEmpty(name)) {
            String secondName = name.substring(1, name.length());
            ((TextView) findViewById(R.id.tv_card_owner)).setText(secondName + " * ");
        }
    }


    // 字体大小不跟随系统
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(getConfigurationContext(newBase));
    }

    private static Context getConfigurationContext(Context context) {
        Configuration configuration = context.getResources().getConfiguration();
        configuration.fontScale = 1;
        return context.createConfigurationContext(configuration);
    }

   /* @Override
    public Resources getResources() {
        Resources res=super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics());
        return res;
    }*/

}